home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Applications
/
Fixation 1.3
/
preffile.c
< prev
next >
Wrap
Text File
|
1996-01-27
|
3KB
|
112 lines
/*
preffile.c by Adam Miller (miller@minerva.cis.yale.edu), 1994
A very easy and modular way to keep a pref file in the System Folder. It's not
perfect, but it seems to work okay for me. If the system is to old, it'll
just put the pref file in the current folder, but will still work. There's
less error checking than there could be, for sure.
To make the file, just call DoSavePrefs, pass a pointer to your data, and the
length of it. To get the prefs, call DoReadPrefs in the same way. DoReadPrefs
returns the number of bytes it read, so if you change versions and the new version
uses more data, it's an easy way to check.
The way I use these functions is to have a struct like PrefStruct. To save, I
fill it with the relevant values and call
DoSavePrefs((Ptr) &MyPrefStruct, sizeof(PrefStruct));
Similar to read, of course.
Report any bugs you find.
*/
#include "preffile.h"
#include <Files.h>
//#define preffilename "\pFixation Prefs" // change this
#define preffilecreator 'fxtn' // and this
#define preffiletype 'pref'
prefStruct gPrefs;
// creates the file
static OSErr DoCreatePrefsFile(short VRefNum, long DirID, Str255 preffilename)
{
return HCreate(VRefNum, DirID, preffilename, preffilecreator, preffiletype);
}
// func returns whether the system is recent enough to find the Sys Folder for us
static int IsFindFolder(void)
{
OSErr Result;
long Feature;
Result = Gestalt(gestaltFindFolderAttr, &Feature);
if (Result == noErr)
return ((Feature & (1 << gestaltFindFolderPresent)) != 0); // find relevant bit
return Result;
}
// read the prefs in from the file
long DoReadPrefs(Ptr p, long length, Str255 preffilename)
{
short VRefNum;
long DirID;
short fref;
OSErr result;
if (IsFindFolder())
result = FindFolder(kOnSystemDisk, kPreferencesFolderType,
kDontCreateFolder, &VRefNum, &DirID);
else
result = -1;
if (result) { // set directory to current directory, tough luck sonny
VRefNum = 0;
DirID = 0;
}
result = HOpen(VRefNum, DirID, preffilename, fsCurPerm, &fref);
if (result) // no existing file, forget it
return false;
result = FSRead(fref, &length, p); // get info
result = FSClose(fref);
fref = 0; // so we don't wipe out any disks (see THINK ref)
return length; // let them know how long the file is (can only be shorter, of course)
}
// write our data to the pref file
void DoSavePrefs(Ptr p, long length, Str255 preffilename)
{
short VRefNum;
long DirID;
short fref;
OSErr result;
if (IsFindFolder())
result = FindFolder(kOnSystemDisk, kPreferencesFolderType,
kDontCreateFolder, &VRefNum, &DirID); // find System Folder
else
result = -1;
if (result) { // set directory to current directory, tough luck sonny
VRefNum = 0;
DirID = 0;
}
result = HOpen(VRefNum, DirID, preffilename, fsCurPerm, &fref);
if (result) { // no existing file, make a new one
result = DoCreatePrefsFile(VRefNum, DirID, preffilename);
if (result)
return; // canna do it
result = HOpen(VRefNum, DirID, preffilename, fsCurPerm, &fref);
if (result)
return; // couldn't open it
}
result = FSWrite(fref, &length, p); // get info
if (!result)
result = SetEOF(fref, length); // just in case
result = FSClose(fref);
result = FlushVol(0, 0);
fref = 0; // so we don't wipe out any disks (see THINK ref)
}